#!/bin/bash

set -o errexit
set -o xtrace

test_dir=$(realpath $(dirname $0))
. ${test_dir}/../functions

create_infra $namespace

desc 'create PXC cluster'

newpass="test-password"
newpassencrypted=$(echo -n "$newpass" | base64)

cluster="some-name"
spinup_pxc "$cluster" "$conf_dir/$cluster.yml"

desc 'test root'
patch_secret "my-cluster-secrets" "root" "$newpassencrypted"
sleep 15
compare_mysql_cmd "select-4" "SHOW TABLES;" "-h $cluster-proxysql -uroot -p$newpass"



desc 'test proxyadmin'
kubectl_bin patch pxc some-name --type=merge -p="{\"spec\":{\"proxysql\":{\"size\":3}}}"
sleep 15
wait_cluster_consistency "$cluster" 3 3
patch_secret "my-cluster-secrets" "proxyadmin" "$newpassencrypted"
sleep 15
wait_cluster_consistency "$cluster" 3 3
compare_mysql_cmd_local "select-2" "SHOW TABLES;" "-h127.0.0.1 -P6032 -uproxyadmin -p$newpass" "$cluster-proxysql-0" "" -c'proxysql'
compare_mysql_cmd_local "select-2" "SHOW TABLES;" "-h127.0.0.1 -P6032 -uproxyadmin -p$newpass" "$cluster-proxysql-1" "" -c'proxysql'
compare_mysql_cmd_local "select-2" "SHOW TABLES;" "-h127.0.0.1 -P6032 -uproxyadmin -p$newpass" "$cluster-proxysql-2" "" -c'proxysql'



desc 'test xtrabackup'
kubectl_bin patch pxc some-name --type=merge -p="{\"spec\":{\"proxysql\":{\"size\":1}}}"
patch_secret "my-cluster-secrets" "xtrabackup" "$newpassencrypted"
sleep 30
wait_cluster_consistency "$cluster" 3 2
compare_mysql_cmd_local "select-3" "SHOW DATABASES;" "-h localhost -uxtrabackup -p$newpass" "$cluster-pxc-0" "" -c'pxc'

desc 'test clustercheck'
patch_secret "my-cluster-secrets" "clustercheck" "$newpassencrypted"
sleep 30
wait_cluster_consistency "$cluster" 3 2
compare_mysql_cmd_local "select-5" "SHOW DATABASES;" "-h localhost -uclustercheck -p$newpass" "$cluster-pxc-0" "" -c'pxc'

desc 'test monitor'
patch_secret "my-cluster-secrets" "monitor" "$newpassencrypted"
sleep 30
wait_cluster_consistency "$cluster" 3 2
compare_mysql_cmd "select-4" "SHOW TABLES;" "-h $cluster-proxysql -umonitor -p$newpass"

desc 'test operator'
patch_secret "my-cluster-secrets" "operator" "$newpassencrypted"
sleep 30
wait_cluster_consistency "$cluster" 3 2
compare_mysql_cmd "select-4" "SHOW TABLES;" "-h $cluster-proxysql -uoperator -p$newpass"

desc 'change secret name'
kubectl_bin patch pxc $cluster --type merge --patch '{"spec": {"secretsName":"my-cluster-secrets-2"}}'
sleep 30
wait_cluster_consistency "$cluster" 3 2
newpass="test-password2"
newpassencrypted=$(echo -n "$newpass" | base64)

desc 'test new operator'
patch_secret "my-cluster-secrets-2" "operator" "$newpassencrypted"
sleep 30
wait_cluster_consistency "$cluster" 3 2
compare_mysql_cmd "select-4" "SHOW TABLES;" "-h $cluster-proxysql -uoperator -p$newpass"

newpass=$(getSecretData "my-cluster-secrets-2" "root")
desc 'test new users sync'
run_mysql \
        "CREATE USER 'testsync'@'%' IDENTIFIED BY '$newpass';" \
        "-h $cluster-pxc -uroot -p$newpass"
sleep 20
compare_mysql_cmd "select-4" "SHOW TABLES;" "-h $cluster-proxysql -utestsync -p$newpass"

pass=$(getSecretData "internal-some-name" "operator")
desc 'check secret without operator'
kubectl_bin apply \
        -f "$test_dir/conf/secrets.yml"
sleep 15
compare_mysql_cmd "select-4" "SHOW TABLES;" "-h $cluster-proxysql -uoperator -p$pass"

newpass="test-password2"
newpassencrypted=$(echo -n "$newpass" | base64)
spinup_pxc "$cluster" "$test_dir/conf/some-name.yml" 3 10
wait_cluster_consistency "$cluster" 3 3
patch_secret "my-cluster-secrets" "monitor" "$newpassencrypted"
sleep 15
wait_cluster_consistency "$cluster" 3 3
compare_mysql_cmd "select-3" "SHOW DATABASES;" "-h $cluster-haproxy -umonitor -p$newpass"

destroy "${namespace}"
